package com.hitqz.robot.biz.flow.component.agv;


import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.util.concurrent.RateLimiter;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.dto.warning.AddWarningDto;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

@Slf4j
@LiteflowComponent(value = "AgvGoodsBuffersDoubleCheck", name = "[轮对/轮轴]缓存架位置存/取二次校验")
@AllArgsConstructor
public class AgvGoodsBuffersDoubleCheck extends NodeComponent {

    private final RestTemplate restTemplate;

    private static final String url = "http://127.0.0.1:9999/admin/agv/checkPickBlock?bindPosName=01N01&num=01N02&operateType=01N03";

    @Override
    public void process() throws Exception {
        LiteFlowUtil.checkInterruptFlag();
        FlowContextDto dto = this.getRequestData();
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        log.info("缓存架位置存/取二次校验==》");
        String source = Optional.ofNullable(ticketsDto.getModalData().get("sourcePosName"))
                .orElse(ticketsDto.getModalData().get("sourcePosCode")).toString();
        String target = Optional.ofNullable(ticketsDto.getModalData().get("targetPosName"))
                .orElse(ticketsDto.getModalData().get("targetPosCode")).toString();
        String srcnum = String.valueOf(ticketsDto.getModalData().get("srcnum"));
        String operateType = Optional.ofNullable(ticketsDto.getModalData().get("sourcePosName"))
                .orElse(ticketsDto.getModalData().get("operateType")).toString();
        if ("put".equals(operateType) || "take".equals(operateType)) {
            String uri = url.replace("01N01", "put".equals(operateType) ? target : source)
                    .replace("01N02", srcnum)
                    .replace("01N03", operateType);
            JSONObject result = restTemplate.postForObject(uri, "", JSONObject.class);
            int code = result.getInteger("code");
            String msg = result.getString("msg");
            RateLimiter warningLimiter = RateLimiter.create(1);
            while (code == 1) {
                if (warningLimiter.tryAcquire(600)) {//10分钟告警一次
                    AddWarningDto addWarningDto = new AddWarningDto();
                    addWarningDto.setCode("0");
                    addWarningDto.setTitle("任务执行异常");
                    addWarningDto.setContent(msg);
                    addWarningDto.setSuggestion("请排查");
                    addWarningDto.setLevel(1);
                    SpringUtil.getApplicationContext().publishEvent(addWarningDto);
                }
                TimeUnit.SECONDS.sleep(1);
            }
        } else {
            log.info("任务不涉及缓存架，跳过");
        }
    }

}
